/**
 * Created by haolw on 5/10/17.
 */

bookApp.controller('BookClassificationCtrl', function($scope, $rootScope, $http, $filter,ngTableParams,$resource, $modal) {

    $scope.paginationConf={
        currentPage:1,
        totalItems:0,
        itemsPerPage: 30
    };

    $scope.bookTypeTableParams = new ngTableParams({
        page: 1,
        count: 100
    },{
        total: 0,
        counts:[],
        getData: function ($defer, params) {
            var queryParams = {pageNo:$scope.paginationConf.currentPage,pageSize:$scope.paginationConf.itemsPerPage};
            $http.get("/api/book/classification",{params:queryParams}).success(function(data){
                if(data.code == 200){
                    $scope.bookTypes = data.data;
                    //$scope.paginationConf.totalItems = data.data.totalCount;
                    $defer.resolve($scope.bookTypes);
                }
            });
        }
    });

    $scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage', function () {
        $scope.bookTypeTableParams.reload();
    });

    $scope.openAddBookTypeModal = function(){
        var modalInstance = $modal.open({
            templateUrl : 'bookTypeModal.html',
            controller : 'bookTypeModalCtrl',
            size : 'md',
            resolve : {
                bookType : function(){
                    return {};
                }
            }
        });
        modalInstance.result.then(function () {
            $scope.bookTypeTableParams.reload();
        },function () {
            $scope.bookTypeTableParams.reload();
        });
    }


    $scope.openEditBookTypeModal = function (bookType) {
        var modalInstance = $modal.open({
            templateUrl : 'bookTypeModal.html',
            controller : 'bookTypeModalCtrl',
            size : 'md',
            resolve : {
                bookType : function(){
                    return bookType;
                }
            }
        });
        modalInstance.result.then(function () {
            $scope.bookTypeTableParams.reload();
        },function () {

        });
    }

    $scope.openDelBookTypeModal = function (bookType) {
        var modalInstance = $modal.open({
            templateUrl : 'delBookTypeModal.html',
            controller : 'delBookTypeModalCtrl',
            size : 'md',
            resolve : {
                bookType : function(){
                    return bookType;
                }
            }
        });
        modalInstance.result.then(function () {
            $scope.bookTypeTableParams.reload();
        },function () {

        });
    }
});

bookApp.controller('bookTypeModalCtrl', function($scope, $rootScope, $http, $filter,$resource, $modalInstance,bookType) {
    $scope.bookType = angular.copy(bookType);
    $scope.save = function(){
        if($scope.bookType.id){
            $http.put("/api/book/classification",$scope.bookType).success(function (data) {
                if(data.status == 400){
                    $scope.errorMsg = "输入信息消息，请重新";
                    return;
                }
                $modalInstance.close($scope.bookType);
            }).error(function (data) {
                if(data.status == 400){
                    $scope.errorMsg = "输入信息消息，请重新";
                }
            });
        }else{
            $http.post("/api/book/classification",$scope.bookType).success(function (data) {
                $modalInstance.close();
            }).error(function (data) {
                if(data.status == 400){
                    $scope.errorMsg = "输入信息消息，请重新";
                }
            });
        }


    }
    $scope.cancel = function(){
        $modalInstance.dismiss();
    }

});

bookApp.controller('delBookTypeModalCtrl', function($scope, $rootScope, $http,$modalInstance,bookType) {

    $scope.ok = function () {
        $http.delete("/api/book/classification/"+bookType.id).success(function () {
            $modalInstance.close();
        });
    }

    $scope.cancel = function () {
        $modalInstance.dismiss();
    }
});